﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject;
using Jalal.Library.Utility;
using Jalal.Library.Mail;
using System.Collections.Specialized;
using log4net;

public partial class Admin_ReadReplyMessage : AdminBasePage
{
    private int DataId
    {
        get
        {
            if (ViewState["DataId"] == null)
            {
                int id = 0;
                ViewState["DataId"] = id;
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    if (int.TryParse(Request.QueryString["id"], out id))
                    {
                        ViewState["DataId"] = id;
                    }
                    else
                        uccResponse.SetFailMessage("Invalid item selected, no such message found");
                }
                else
                    uccResponse.SetFailMessage("No message selected");
            }
            return (int)ViewState["DataId"];
        }
        set { ViewState["DataId"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        try
        {
            Contact contact = Contact.Load(DataId);
            if (contact != null)
            {
                lblContactedBy.Text = contact.ClientName;
                lblContactDateTime.Text = CommonUtility.FormatedDateTimeString(contact.ContactDateTime);
                lblEmail.Text = contact.ClientEmail;
                lblOrganization.Text = contact.ClientOrganization;
                lblPhone.Text = contact.ClientPhone;
                lblSubject.Text = contact.ContactSubject;
                lblWebsite.Text = contact.ClientWebsite;
                ltlMessage.Text = contact.ContactDescription;

                contact.IsRead = true;
                contact.Update();
            }
            else
                uccResponse.SetFailMessage("Invalid item selected, no such message found");
        }
        catch (Exception ex)
        {
            // Write the log into handled exception list
            ILog log = LogHelper.GetHandledFileLogger("ReadReplyMessage");
            if (log.IsErrorEnabled)
                log.Error("Failed to load message for reply", ex);
        }
    }

    protected void SendReply_Click(object sender, EventArgs e)
    {
        try
        {
            Contact contact = Contact.Load(DataId);
            NameValueCollection items = new NameValueCollection();
            items.Add("Subject", tbxSubject.Text);
            items.Add("ReceiverName", contact.ClientName);
            items.Add("ReceiverEmail", contact.ClientEmail);
            if (MailSender.SendHtmlMaill(Settings.MailSettings, fckMessage.Value, items))
                uccResponse.SetSuccessMessage("Mail sent to client");
            else
                uccResponse.SetFailMessage("Mail send failed");
        }
        catch (Exception ex)
        {
            // Write the log into handled exception list
            ILog log = LogHelper.GetHandledFileLogger("ReadReplyMessage");
            if (log.IsErrorEnabled)
                log.Error("Failed to send reply", ex);
        }
    }

    protected void GoBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(CustomSiteMap.ContactList);
    }
}
